package com.smartwebx.ha.dal.util;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Order(0)
public class MultipleDataSourceAspectAdvice {
//	Map<Dialect, SqlUtil.Parser> sqlUtilParserMap = Maps.newHashMap();

	@Around("execution(* com.smartwebx.ha.dal.realtime.dao.*.*(..)) || execution(* com.smartwebx.ha.dal.realtime.prod.*.*(..))")
	public Object doAroundOracle(ProceedingJoinPoint jp) throws Throwable {
		MultipleDataSource.setDataSourceKey("realtimeDataSource");

//		Dialect key = Dialect.oracle;
//		SqlUtil.Parser simpleParser = null;
//		if (sqlUtilParserMap.containsKey(key)) {
//			simpleParser = sqlUtilParserMap.get(key);
//		} else {
//			simpleParser = SqlUtil.SimpleParser.newParser(key);
//			sqlUtilParserMap.put(key, simpleParser);
//		}
//		
//		FieldUtils.writeField(SqlParser.class, "simpleParser",
//				simpleParser, true);

		return jp.proceed();
	}

	@Around("execution(* com.smartwebx.ha.dal.feature.dao.*.*(..))")
	public Object doAroundFeature(ProceedingJoinPoint jp) throws Throwable {
		MultipleDataSource.setDataSourceKey("featureDataSource");
		return jp.proceed();
	}

	@Around("execution(* com.smartwebx.ha.dal.assign.dao.*.*(..))")
	public Object doAroundMysql(ProceedingJoinPoint jp) throws Throwable {
		MultipleDataSource.setDataSourceKey("mysqlDataSource");
		
//		Dialect key = Dialect.mysql;
//		SqlUtil.Parser simpleParser = null;
//		if (sqlUtilParserMap.containsKey(key)) {
//			simpleParser = sqlUtilParserMap.get(key);
//		} else {
//			simpleParser = SqlUtil.SimpleParser.newParser(key);
//			sqlUtilParserMap.put(key, simpleParser);
//		}
//		FieldUtils.writeField(SqlParser.class, "simpleParser",
//				simpleParser, true);
		
		return jp.proceed();
	}
}